package emas.impl;

import java.util.ArrayList;
import java.util.List;

import emas.inf.*;

public class EmasIsland<Genotype> implements Island<Genotype> {
	private int _energy;
	private List<Agent<Genotype>> _agents;
	public List<Agent<Genotype>> _agentsOp; //private
	public List<Agent<Genotype>> _agentsMigr; //private

	public EmasIsland(int aInitialEnergy) {
		this._agents = new ArrayList<Agent<Genotype>>();
		this._agentsOp = new ArrayList<Agent<Genotype>>();
		this._agentsMigr = new ArrayList<Agent<Genotype>>();
		this._energy = aInitialEnergy;
	}

	public void addAgent(Agent<Genotype> aAgent) {
		for(Agent<Genotype> agent: this.getAgentsOp()) {
			agent.addNeighbor(aAgent);
		}
		aAgent.addNeighbors(this.getAgentsOp());

		this._agentsOp.add(aAgent);
	}

	public void addAgentMigr(Agent<Genotype> aAgent) {
		this._agentsMigr.add(aAgent);
	}

	public List<Agent<Genotype>> getAgents() {
		return this._agents;
	}

	public List<Agent<Genotype>> getAgentsOp() {
		return this._agentsOp;
	}

	public void removeAgent(Agent<Genotype> aAgent) {
		for(Agent<Genotype> agent: this.getAgentsOp()) {
			agent.removeNeigbor(aAgent);
		}
		this._agentsOp.remove(aAgent);
	}

	public void increaseEnergy(int aAmount) {
		this._energy += aAmount;
	}

	public void copyList() {
		this._agents.removeAll(_agents);

		//System.out.println("copyList rozmiar _agentsMigr " + this._agentsMigr.size());
		for(Agent<Genotype> agentMigr: this._agentsMigr) {
			for(Agent<Genotype> agent: this.getAgentsOp())
				agent.addNeighbor(agentMigr);
			
			this._agentsOp.add(agentMigr);
		}
		
		for(Agent<Genotype> agent: this._agentsOp)
			this._agents.add(agent);


		
		/*
		 for(Agent<Genotype> agent: this.getAgentsOp()) {
			agent.addNeighbor(aAgent);
		}
		aAgent.addNeighbors(this.getAgentsOp());

		this._agentsMigr.add(aAgent);

		 */
	}

	//zwraca energie wyspy do srodowiska i zeruje jej energie
	public int receiveEnergy() {
		//System.out.println("receiveEnergy Wyspa: energia " + this._energy);
		int tmpEnergy = this._energy;
		this._energy = 0;

		return tmpEnergy;
	}

	public int getEnergy() {
		return this._energy;
	}


}